//
// Created by ChenJi on 2021/8/2.
//

#ifndef HC_TERMINAL_BC26DRIVER_H
#define HC_TERMINAL_BC26DRIVER_H

#include "main.h"
#include "UartController.h"
typedef struct {
    char *originValuePtr;
    char *StringValuePtr;
    int32_t numValue;
    uint8_t value_len;
    uint8_t invalidValue_len;
}Parameter;
typedef struct {
    Parameter params[20];
    uint8_t validParamsAmount;
} ParameterReader;
typedef struct {
    uint8_t CSQ;
    uint8_t online;
    uint8_t MQTTConnected;
    uint8_t SocketConnected;
    uint8_t Terminal_ready;

}BC26_States;
typedef enum {
    BC26_StandBy_Reset = 0,
    BC26_Wait_for_NET_Register= 1,
    BC26_Wait_for_Socket_Connect_Send = 2,
    BC26_Wait_for_Socket_Connect_Reply = 3,
    BC26_Wait_for_MQTT_Connect_Send = 4,
    BC26_Wait_for_MQTT_Connect_Reply = 5,
    BC26_Wait_for_Subscribe_Send = 6,
    BC26_Wait_for_Subscribe_Reply = 7
}BC26_Process;
typedef struct{
    uint8_t retry_times;
    uint8_t time_out_second;
    uint8_t enable;
    uint8_t isFault;
    uint8_t now_time;
    BC26_Process process;
}BC26_Connect_MQTT_Setup;
typedef struct{
    uint8_t time;
}BC26_KeepAlive;
typedef struct {
    uint8_t waiting_for_success;
    uint8_t free;
    char* topic;
    char* message;
    uint8_t retryTimes;
}BC26_MQTT_PublishManager;
void BC26_Init();
void bc26_sendCGATT();

void bc26_appendCMDQ( char* cmd);
void bc26_appendCMD( char* cmd);
void bc26_appendParamNumber(int num,uint8_t last);
void bc26_appendParamString(char* str,uint8_t last);
void bc26_sendCSQ();

void BC26Init();
void bc26_atAppendString(char* str);
void bc26_clearATBuffer();
void bc26_atAppendNumber(int num);
void bc26_atAppendHexNumber(int num);
void bc26_sendAtBuffer();

void onOKPocketReceive(char *cmd, uint8_t len);

void onERRORPocketReceive(char *cmd, uint8_t len);

void onValueReceive(char *cmd, uint8_t cmd_len, char *val, uint8_t val_len);
void loadParameter(char* value,uint8_t value_len);
void bc26_Subscribe(char* name,uint8_t qos);
void bc26_ConnectMQTT(char* name);
void bc26_ConnectSocket(char* server,uint16_t port);

__weak void bc26_onTimeout();
void setBC26SetupTimeout(uint8_t timeout);
__weak void onBC26RegisteredEvent();
__weak void onBC26unRegisteredEvent();
__weak void onBC26SocketDisconnectEvent(uint8_t err_code);
__weak void onBC26SocketConnectEvent();
__weak void onBC26SocketConnectFailedEvent(uint8_t err_code);
__weak void onMQTTConnectEvent();
__weak void onMQTTConnectFailedEvent(uint8_t code);
__weak void onMQTTSubscribetEvent();
__weak void onMQTTSubscribeFailedEvent(uint8_t code);
__weak void onMQTTPublishEvent();
__weak void onMQTTPublishFailedEvent(uint8_t code);
__weak void onMQTTPublishErrorEvent(uint8_t code);
__weak void onMQTTPublishTryoutEvent(BC26_MQTT_PublishManager* manager);

void    BC26_Setup_ResetAndSetup();
void sendKeepAlivePocket();
uint8_t bc26_publishTopic(char* topic,char* value);
void bc26_publishTopicDirect(char* topic,char* value);

#endif //HC_TERMINAL_BC26DRIVER_H
